package com.rds.benchmark.operator;

import redis.clients.jedis.ClusterPipeline;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.commands.JedisCommands;

import java.io.IOException;

public class OneWriteNineReadOperator extends TypeOperatorBase {

    private Object conn;

    private String key;

    private String value;

    @Override
    public void init(Object conn, String key, int idx,String mode) {
        this.conn = conn;
        this.key = key;
        this.value = key + "-value";
    }

    @Override
    public Long clear() {
        return ((JedisCommands) conn).del(key);
    }

    @Override
    public void operate() throws IOException {
//        for (int k = 0; k < 100; ++k) {
//            for (int j = 0; j < 10; j++) {
//                if (j == 0) {
//                    conn.set(key, value);
//                } else {
//                    conn.get(key);
//                }
        if (conn instanceof Jedis) {
            Jedis jedis = (Jedis) conn;
            Pipeline pipeline = jedis.pipelined();
            pipeline.set(key, value);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.syncAndReturnAll();
        } else if (conn instanceof JedisCluster) {
            JedisCluster cluster = (JedisCluster) conn;
            ClusterPipeline pipeline = cluster.pipelined();
            pipeline.set(key, value);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.get(key);
            pipeline.sync();
        }
        times = times + 10;
//            }
//        }
    }
}
